package com.carb.controller.admin;

import com.carb.annotation.RequiredRoles;
import com.carb.constant.AuthorityConstant;
import com.carb.dto.CarAddDTO;
import com.carb.dto.CarPageQueryDTO;
import com.carb.dto.CarUpdateDTO;
import com.carb.entity.Car;
import com.carb.result.PageResult;
import com.carb.result.Result;
import com.carb.service.ICarService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 车辆管理 前端控制器
 * </p>
 *
 * @author XuChaoyang
 * @since 2025-01-18
 */
@RestController("adminCarController")
@RequestMapping("/admin/car")
@Tag(name = "车辆管理相关接口")
@Slf4j
public class CarController {

    @Resource
    private ICarService carService;

    /**
     * 新增车辆
     * @param carAddDTO 新增车辆信息
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER})
    @PostMapping("/add")
    @Operation(summary = "新增车辆")
    public Result<String> addCar(@RequestBody CarAddDTO carAddDTO) {
        log.info("新增车辆：{}", carAddDTO);
        carService.addCar(carAddDTO);
        log.info("新增车辆成功！");
        return Result.success();
    }

    /**
     * 车辆分页查询
     * @param carPageQueryDTO 查询条件
     * @return 车辆分页结果
     */
    @GetMapping("/page")
    @Operation(summary = "车辆分页查询")
    public Result<PageResult<Car>> pageQuery(CarPageQueryDTO carPageQueryDTO) {
        log.info("车辆分页查询：{}", carPageQueryDTO);
        PageResult<Car> pageResult = carService.pageQuery(carPageQueryDTO);
        log.info("车辆分页查询成功！");
        return Result.success(pageResult);
    }

    /**
     * 根据车辆id查询车辆信息
     * @param id 车辆id
     * @return 查询结果
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据车辆id查询车辆信息")
    public Result<Car> getCarById(@PathVariable @Parameter(description = "车辆id") Long id) {
        log.info("根据车辆id查询车辆信息，车辆id：{}", id);
        Car car = carService.getCarById(id);
        log.info("根据车辆id查询车辆信息成功！");
        return Result.success(car);
    }

    /**
     * 修改车辆信息
     * @param carUpdateDTO 修改的车辆信息
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER})
    @PutMapping
    @Operation(summary = "修改车辆信息")
    public Result<String> updateCar(@RequestBody CarUpdateDTO carUpdateDTO) {
        log.info("修改车辆信息：{}", carUpdateDTO);
        carService.updateCar(carUpdateDTO);
        log.info("修改车辆信息成功！");
        return Result.success();
    }

    /**
     * 删除车辆
     * @param id 车辆id
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER})
    @DeleteMapping("/{id}")
    @Operation(summary = "删除车辆")
    public Result<String> deleteCar(@PathVariable @Parameter(description = "车辆id") Long id) {
        log.info("删除车辆，车辆id：{}", id);
        carService.deleteCar(id);
        log.info("删除车辆成功！");
        return Result.success();
    }

}
